//
//  GoodsDetailHeader.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/30.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import LLCycleScrollView

class GoodsDetailHeader: UIView {
    
    var bannerView : LLCycleScrollView!
    
    var nameLabel:UILabel!
    
    var priceLabel:UILabel!
    
    var bazaarLabel:UILabel!
    
    var spareLabel:UILabel!  // 节省的钱label
    
    var discountLabel:UILabel! // 折扣label
    
    var playerClosure:TouchBlock!


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.nav_color
        initContent()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    private func initContent(){
        bannerView = LLCycleScrollView.llCycleScrollViewWithFrame(CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenWidth/(375/305)))
        bannerView.playerBtnClick { [weak self] in
            if self?.playerClosure != nil {
                self?.playerClosure()
            }
        }
        // 设置图片显示方式=UIImageView的ContentMode
        bannerView.imageViewContentMode = .scaleToFill
        // 设置当前PageControl的样式 (.none, .system, .fill, .pill, .snake)
        bannerView.customPageControlStyle = .fill
        bannerView.customPageControlTintColor = UIColor.init(hexColor: "#f7c201")
        bannerView.customPageControlInActiveTintColor = UIColor.init(hexColor: "#ffffff")
        self.addSubview(bannerView)
        
        self.nameLabel = Utils.labelWith(frame: CGRect.init(x: 12, y: self.bannerView.bottom + 19, width: UIScreen.ScreenWidth - 24, height: 16), font: UIFont.systemFont(ofSize: 17), textAlignment: NSTextAlignment.center, textColor: "#333333", text: "")
        self.addSubview(self.nameLabel)
        
        var width = "¥20.00/斤".rect(withFont: UIFont.systemFont(ofSize: 13), size: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 17.5)).width + 5
        self.priceLabel = Utils.labelWith(frame: CGRect.init(x: (self.width - width) * 0.5, y: self.nameLabel.bottom + 18, width: width, height: 17.5), font: UIFont.systemFont(ofSize: 13), textAlignment: NSTextAlignment.center, textColor: "#ed4848", text: "")
        self.addSubview(self.priceLabel)
        
        self.bazaarLabel = Utils.labelWith(frame: CGRect.init(x: self.nameLabel.left, y: self.priceLabel.bottom + 9, width: self.nameLabel.width, height: 11.5), font: UIFont.systemFont(ofSize: 12), textAlignment: NSTextAlignment.center, textColor: "#666666", text: "")
        self.addSubview(self.bazaarLabel)
        
        self.spareLabel = Utils.labelWith(frame: CGRect.init(x: UIScreen.ScreenWidth - 115, y: 0, width: 115, height: 25), font: UIFont.systemFont(ofSize: 11), textAlignment: .center, textColor: "ffffff", text: "")
        self.spareLabel.backgroundColor = UIColor.init(hexColor: "#f7c201")
        self.spareLabel.corner(byRoundingCorners: [.topLeft,.bottomLeft], radii: 12.5)
        self.spareLabel.isHidden = true
        self.addSubview(self.spareLabel)
        self.spareLabel.centerY = self.priceLabel.centerY
        
        width = "  5.5折  ".rect(withFont: UIFont.systemFont(ofSize: 12), size: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 20)).width + 5
        self.discountLabel = Utils.labelWith(frame: CGRect.init(x: self.priceLabel.right + 10, y: 0, width: width, height: 20), font: UIFont.systemFont(ofSize: 12), textAlignment: .center, textColor: "#ffffff", text: "")
        self.discountLabel.isHidden = true
        self.discountLabel.backgroundColor = UIColor.init(hexColor: "#fb9441")
        self.discountLabel.setCornerRadio(radio: 5, isMask: true)
        self.discountLabel.centerY = self.priceLabel.centerY
        self.addSubview(self.discountLabel)
        
        self.height = self.bazaarLabel.bottom + 15.5
        
    }
    
    func updateSubViews(model:GoodsDetailModel,type:GoodsDetailType){
        self.nameLabel.text = model.goods?.name
        self.bannerView.imagePaths = model.banners!
        
        var price = ""
        if model.goods?.isDiscount == 1 {
            price = "\(model.goods!.discountMoney)"
        }else if model.goods!.isActivity == 1 {
            price = "\(model.goods!.activityMoney)"
        }else if model.goods!.peddlersPrice != nil {
            price = model.goods!.peddlersPrice!
        }else{
            price = "\(model.goods!.price)"
        }
        
        let width = ("¥\(price)/" + (model.goods?.spec)!).rect(withFont: UIFont.systemFont(ofSize: 13), size: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 17.5)).width + 20
        self.priceLabel.attributedText = Utils.changeLabelTextFontSize(normalString: "¥\(price)/" + (model.goods?.spec)!, changeString: "\(price)".getPriceInteger(), font: UIFont.systemFont(ofSize: 22))
        self.priceLabel.width = width
        self.priceLabel.centerX = self.centerX
        self.bazaarLabel.attributedText = Utils.labelTextDeleLine(normalString: "市场价：￥\(model.goods?.marketPrice ?? "")", deleString: "￥\(model.goods?.marketPrice ?? "")")
        self.spareLabel.attributedText = Utils.changeLabelTextFontSize(normalString:"    每套立省\((model.goods?.price)! - (model.goods?.activityMoney)!)元    ", changeString: "\((model.goods?.price)! - (model.goods?.activityMoney)!)", font: UIFont.systemFont(ofSize: 17))
        
        if type == .spare {
            self.spareLabel.isHidden = false
        }
        
        if type == .discount {
            let disCount = String.init(format: "%.2f", model.goods!.discountMoney / model.goods!.price)
            let width = (" " + disCount + "折" + "  ").rect(withFont: UIFont.systemFont(ofSize: 12), size: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 20)).width + 5
            self.discountLabel.width = width
            self.discountLabel.left = self.priceLabel.right + 5
            self.discountLabel.text = " " + disCount + "折" + "  "
            self.discountLabel.isHidden = false
        }
        
        if model.goods!.video != "" {
           self.bannerView.isShowPlayerBtn = true
        }
    }

}

